package com.tozed.widget;

import com.tozed.test.R;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TitleBar extends RelativeLayout
{
	private ImageButton mBtnLeft;
	private ImageButton mBtnRight;
	private TextView mTitle;
	private FrameLayout mTitleContainer;
	  
	public TitleBar(Context context){
		this(context, null);
	}
	
	public TitleBar(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}

	public TitleBar(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		
		setBackgroundResource(R.color.app_color);
	    LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);
	    
	    mBtnLeft  = ((ImageButton)findViewById(R.id.title_bar_left));
	    mBtnRight = ((ImageButton)findViewById(R.id.title_bar_right));
	    mTitle = ((TextView)findViewById(R.id.title_bar_text));
	    mTitleContainer = ((FrameLayout)findViewById(R.id.title_bar_title_container));
	    
	    setLeftBtnOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				if (!(mBtnLeft.getContext() instanceof Activity))
			    	return;
				
				final Activity theActivity = ((Activity)mBtnLeft.getContext());
				
		        InputMethodManager imm = (InputMethodManager)theActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		        imm.hideSoftInputFromWindow(mBtnLeft.getWindowToken(), 0);
		        
		        new Handler().postDelayed(new Runnable(){
		            public void run() { 
		            	theActivity.finish();
		            }    
		         }, 30);
			}
		});
	}
	
	public final void hideLeftButton(){
		if (mBtnLeft == null) return;
		mBtnLeft.setVisibility(View.GONE);
	}
	
	public final void hideRightButton(){
		if (mBtnRight == null) return;
		mBtnRight.setVisibility(View.GONE);
	}
	
	public final void setRightButtonRes(int resId){
		if (mBtnRight == null) return;
		mBtnRight.setImageResource(resId);
	}
	
	public final void setLeftBtnOnClickListener(View.OnClickListener listerner){
		if (mBtnLeft == null) return;
		mBtnLeft.setOnClickListener(listerner);
	}
	
	public final void setRightBtnOnClickListener(View.OnClickListener listerner){
		if (mBtnRight == null) return;
		mBtnRight.setOnClickListener(listerner);
	}
	
	public final void replaceTitleContainerView(View newView){
		if (newView == null)
			return;
		mTitleContainer.removeAllViews();
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER;
		mTitleContainer.addView(newView, params);
	}
	
	public final void setTiteText(String strTitle){
		if (mTitle == null) return;
		mTitle.setText(strTitle);
	}
	
	public final void setTiteText(int resId){
		if (mTitle == null) return;
		mTitle.setText(resId);
	}
	
	public final void showRightButton(boolean flag){
		if (mBtnRight == null) 
			return;
		
		if (!flag)
			return;
		
		mBtnRight.setVisibility(View.VISIBLE);
	}
	
	public final void displayRightButton(){
		showRightButton(true);
	}
}
